#include<iostream>
#include<string.h>
#include<memory>
#include<algorithm>

using std::cout;
using std::endl;
using std::cin;
using std::uninitialized_copy;

class Computer
{
public:
    Computer()
        :_brand(new char[5]())
    {}
    ~Computer()
    {
        cout<<"xixi"<<endl;
        if(_brand)
        {
            delete []_brand;
        }
        _brand=nullptr;
    }

private:
    char* _brand;
};
template <typename T>
class Vector
{
public:

    Vector();
    ~Vector();
    int size();
    int capacity();

    void print()
    {
        cout<<"*****************"<<endl;
        /* for(auto i=_start;i<_finish;++i) */
        /* { */
        /*     cout<<*i<<" "; */
        /* } */
        cout<<endl;
        cout<<"capacity: "<<capacity()<<endl;
        cout<<"size: "<<size()<<endl;
        cout<<"*****************"<<endl;
        cout<<endl<<endl;
    }

    void push_back( const T& rhs);
    void pop_back();

private:
    void reallocate()
    {
        //最好还需要对0进行判断
        int capa=capacity()>4096*4096? capacity()+4096:2*capacity();
        int si=size();
        //扩容首先申请更大空间
        T* temp =  _alloc.allocate(capa);
        //拷贝数据元素
        if(_start)
        {
            //使用uninitialized_copy
            uninitialized_copy(_start,_finish,temp);
            //循环释放
            while(_start!=_finish)
            {
                _alloc.destroy(--_finish);
            }
            //摧毁申请的空间
            _alloc.deallocate(_start,si);
            //现在之前数据已经处理完成,开始转移
        }
        _start=temp;
        _finish=_start+si;
        _end_of_storage=_start+capa;
    }
private:
    static std::allocator<T> _alloc;
    T *_start;
    T *_finish;
    T * _end_of_storage;
};
template <typename T>
std::allocator<T> Vector<T>:: _alloc;

//无参初始
    template <typename T>
Vector<T>::Vector()
    :_start(nullptr)//无参初始？
    ,_finish(_start)
     ,_end_of_storage(_start)
{
    cout<<"vector 无参初始"<<endl;
}

//析构函数
    template <typename T>
Vector<T>::~Vector()
{
    if(_start)
    {
        int capa=capacity();
        while(_start!=_finish)
        {
            _alloc.destroy(--_finish);
        }
        //摧毁申请的空间
        _alloc.deallocate(_start,capa);
        //现在之前数据已经处理完成,开始转移
    }

    //调用析构函数,析构函数首先destory()，再deallocator()
}

    template <typename T>
int Vector<T>::size()
{
    //输出大小
    return _finish-_start;
}

    template <typename T>
int Vector<T>::capacity()
{
    //输出大小
    return _end_of_storage-_start;
}
//push_back
    template <typename T>
void Vector<T>::push_back(const T & rhs)
{

    //空间判断

    //处理第一次插入
    if(_start==_finish)
    {
        //分配未初始化空间
        _start=_alloc.allocate(1);
        _finish=_start+1;
        _end_of_storage=_start+1;
        _alloc.construct(_start,rhs);
        return ;
    }
    else
    {
        //开始扩容以及插入逻辑
        //扩容
        if(size()==capacity())
        {
            reallocate();
        }
        //插入数据
        _alloc.construct(_finish++,rhs);
    }
}

    template <typename T>
void Vector<T>::pop_back()
{
    //删除元素
    //不回收空间但是删除数据
    _alloc.destroy(--_finish);
}
int main(void)
{
    Vector<Computer> vec;
    cout<<vec.size()<<","<<vec.capacity()<<endl;
    Computer cp1;
    Computer cp2;
    Computer cp3;
    Computer cp4;
    Computer cp5;
    Computer cp6;
    Computer cp7;
    Computer cp8;

    vec.push_back(cp1);
    vec.print();
    vec.push_back(cp2);
    vec.print();
    vec.push_back(cp3);
    vec.print();
    vec.push_back(cp4);
    vec.print();
    /*     cin>>n; */
    /*     vec.push_back(n); */
    /*     vec.print(); */
    /*     cout<<"**********"<<endl; */
    /*     cout<<endl<<endl; */
    /* } */
    }

